import etcd3
import threading
import time
import setting
import json
import datetime
import psutil


class CrawlBaidu(object):
    name = "crawl_baidu_com"

    def run(self, event):
        print(self.name, event)


class CrawlFlsfls(object):
    name = "crawl_flsfls"

    def run(self, event):
        print(self.name, event)


tasks = [CrawlBaidu(), CrawlFlsfls()]


class Reporter(threading.Thread):
    def __init__(self):
        print(setting.ETCD_HOST)
        self.etcd_client = etcd3.client(host=setting.ETCD_HOST)
        threading.Thread.__init__(self)

    def run(self):
        while True:
            self.report()
            time.sleep(10)

    def report(self):
        global tasks

        print("report at {}".format(datetime.datetime.now()))
        task_names = []
        for task in tasks:
            task_names.append(task.name)

        key = "/node/{}".format(setting.NODE_NAME)

        mem = psutil.virtual_memory()
        value = {
            "mem_total": mem.total,
            "mem_used": mem.used,
            "mem_free": mem.free,
            "cpu": psutil.cpu_count(),
            "now": time.time(),
            "tasks": task_names,
        }
        self.etcd_client.put(key, json.dumps(value))


class Tasker(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.etcd_client = etcd3.client(host=setting.ETCD_HOST)

    def register(self):
        global tasks
        for task in tasks:
            self.etcd_client.add_watch_prefix_callback("/rpc/{}_{}".format(task.name, setting.NODE_NAME), task.run)

    def run(self):
        self.register()


def main():
    reporter = Reporter()
    tasker = Tasker()

    reporter.start()
    tasker.start()

    reporter.join()
    tasker.join()


if __name__ == "__main__":
    main()
